#include "managerview_impl.h"
#include <iomanip>
ManagerViewImpl::ManagerViewImpl()
{
	manager = new ManagerImpl;
}
void ManagerViewImpl::Menu(void)
{
	cout << "*******欢迎来到图书管理系统*******" << endl;
	cout << "\e[33m        1、添加书籍               \e[0m" << endl;
	cout << "\e[33m        2、借书    	           \e[0m" << endl;
	cout << "\e[33m        3、还书		           \e[0m" << endl;
	cout << "\e[33m        4、显示书籍	           \e[0m" << endl;
	cout << "\e[33m        5、添加读者               \e[0m" << endl;
	cout << "\e[33m        6、删除读者               \e[0m" << endl;
	cout << "\e[33m        7、显示读者               \e[0m" << endl;
	cout << "\e[33m        8、充值	              \e[0m" << endl;
	cout << "\e[33m        9、退出 		           \e[0m" << endl;
	cout << "**********************************" << endl;
}
void ManagerViewImpl::Run(void)
{
	while(1)
	{
	    system("clear");
		Menu();
		cout << "\e[34m请输入指令：\e[0m";
		char cmd = 0;
		while(1)
		{
			cmd = getch();
			if(cmd >='1' && cmd <='9')
				break;
		}
		cout << cmd << endl;
		system("clear");
		switch(cmd-'0')
		{
			//添加书籍
			case 1:AddBook(); break;
			//借书
			case 2:BorrowBook(); break;
			//换书
			case 3:ReturnBook(); break;
			//显示书籍
			case 4:ShowBook(); break;
			//添加读者
			case 5:AddReader(); break;
			//删除读者
			case 6:DelReader(); break;
			//显示读者
			case 7:ShowReader(); break;
			//充值
			case 8:SetMoney(); break;
			//推出
			case 9:manager->save();return;
		}
	}
}
//设置金额
void ManagerViewImpl::SetMoney(void)
{
	int id = 0;
	cout << "\e[34m请输入读者ID:\e[0m";
	cin >> id;
	int cash = 0;
	cout << "\e[34m请输入充值的金额:\e[0m";
	cin >> cash;
	int money = manager->SetMoney(cash,id);
	if(money == 0)
	{
		cout << "\e[34m没有此人\e[0m" << endl;
	}
	else
		cout << "\e[34m您当前的余额：\e[0m" << money << endl;
	anykey_continue();

}
//增加书籍
void ManagerViewImpl::AddBook(void)
{
	char name[40] = {};
	cout << "\e[34m请输入书名：\e[0m";
	cin >> name;
	int id = generator_BookId();
	Book book(name,id);
	if(manager->AddBook(book))
	{
        cout << "\e[36m添加书籍成功！\e[0m" << endl;
    }
    else
    {
        cout << "\e[31m添加书籍失败！\e[0m" << endl;
    }
	anykey_continue();
}
//添加读者
void ManagerViewImpl::AddReader(void)
{
	char name1[20] = {};
	cout << "\e[34m输入要添加用户的姓名：\e[0m";
	cin >> name1;
	int id1 = generator_ReaderId();
	int money1 = 0;
	cout << "\e[34m输入定金：\e[0m";
	cin >> money1;
	Reader r(name1,id1,money1);
	manager->AddReader(r);
	anykey_continue();
}
//借书
void ManagerViewImpl::BorrowBook(void)
{
	int u_id = 0;
	cout << "\e[34m请输入用户ID：\e[0m";
	cin >> u_id;
	int b_id = 0;
	cout << "\e[34m请输入书名的ID：\e[0m";
	cin >> b_id;
	if(manager->BorrowBook(b_id,u_id))
		cout << "\e[36m借书成功\e[0m" << endl;
	anykey_continue();
}
//还书
void ManagerViewImpl::ReturnBook(void)
{
	int u_id = 0;
	cout << "\e[34m请输入用户ID：\e[0m";
	cin >> u_id;
	int b_id = 0;
	cout << "\e[34m请输入书名的ID：\e[0m";
	cin >> b_id;
	manager->ReturnBook(b_id,u_id);
	anykey_continue();
}

//删除读者
void ManagerViewImpl::DelReader(void)
{
	manager->DelReader();
	anykey_continue();
}

//显示图书
void ManagerViewImpl::ShowBook(void)
{
	manager->ShowBook();
	anykey_continue();
}

//显示读者
void ManagerViewImpl::ShowReader(void)
{
	manager->ShowReader();
	anykey_continue();
}
